<template>
  <div>
    <div id="write">
      <div class="head">
        <el-input v-model="way" size="small" style="width:200px" placeholder="请输入类型"></el-input>
        <div style="display: inline-block">
          <label for="title">标题:</label>
        <el-input v-model="title" size="small" style="width:200px;margin-left:8px;" placeholder="请输入标题"></el-input>

        </div>
        <button @click="cancelWrite">取消</button>
      </div>
      <mavon-editor
        v-model="editContent"
        ref="md"
        :codeStyle="codeStyle"
        @save="save"
        :ishljs="true"
        class="mavonWrite"
      />
    </div>
  </div>
</template>

<script lang="ts">
import { Component,Vue } from "vue-property-decorator";
@Component
export default class write extends Vue {
  content: string = "";
  editContent: string = "";
  codeStyle: string = "vs2015";
  way: string = "vue";
  img: string = "";
  title: string = "";
  id: number = 0;
  text: string = "添加成功";
  showtip: boolean = false;
  msgWay: string = "success";
  cancelWrite() {
    if (this.$route.path === "/blog") { 
      this.$emit("cancelEdit");
    } else {
      this.$router.replace("/blog");
    }
  }
  save(value: string, render: string) {
    if(this.title.trim() === ''||this.way.trim()===''){
      this.$message.error("请输入标题或类型")
      return ;
    }
    if (this.$route.path === "/write") {

      this.$axios
        .post("/blog/add", {
          status: this.$store.state.user.userInfo.status,
          content: render,
          editContent: value,
          way: this.way,
          title: this.title,
        })
        .then((res) => {
          if (res.data.error == -1) {
            this.$message({
              type: "success",
              message: "添加成功",
            });
            (this.$parent as any).showWrite = false;
            (this.$parent as any).getBlogList()
            this.$router.push("/blog");
            
          } else {
            this.$message({
              type: "error",
              message: "添加失败",
            });
          }
        });
    } else if (this.$route.path === "/blog") {
      console.log(render)
      this.$axios
        .post("/blog/update", {
          status: this.$store.state.user.userInfo.status,
          content: render,
          editContent: value,
          id: this.id,
          title:this.title,
          way:this.way
        })
        .then((res) => {
          if (res.data.error == -1) {
            this.$message({
              type: "success",
              message: "更新成功",
            });
            (this.$parent as any).showWrite = false;
            (this.$parent as any).getBlogList()

          } else {
            this.$message({
              type: "error",
              message: "更新失败",
            });
          }
        });
    }
  }
}
</script>

<style lang="less" scoped>
// @import url("../../css/comment/animate.css");
.head {
  text-align: center;
  padding: 15px 20px;
  button {
    width: 100px;
    height: 30px;
    border-radius: 10px;
    background-color: #f56c6c;
    margin-left: 15px;
    border: none;
    color: #000;
    transition: all 0.3s;
    &:hover {
      color: #fff;
      background-color: red;
    }
  }
}

#write {
  margin: 0px auto;
  top: 0;

  width: 90%;
  height: calc(100vh - 120px);
  border-radius: 10px;
  position: relative;
  z-index: 9;
  .mavonWrite {
    height: 90%;
    /deep/ p{
      text-indent: 2em;
    }
  }
}
input {
  border: none;
  background-color: #e5e5e5;
  width: 200px;
  height: 30px;
  margin-left: 15px;
  line-height: 30px;
  padding: 0 15px;
  border-radius: 10px;
}
label {
  margin-left: 15px;
}
select {
  width: 100px;
  height: 30px;
  border-radius: 10px;
  padding-left: 15px;
  background-color: #e5e5e5;
  margin-left: 15px;
  cursor: pointer;
}
.msg {
  z-index: 9999;
}
@media screen and (max-width: 765px) {
  #write {
    padding: 0;
    margin: 0;
    width: 100%;
  }
  .head {
    width: 100%;
    margin: 15px 0;
  }
  input {
    width: 150px;
  }
}
</style>